package com.boding.power.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;

import com.boding.power.common.Constants;
import com.boding.power.dto.CommentDto;
import com.boding.power.model.Comment;
import com.boding.power.model.GoodsPower;
import com.boding.power.utils.DataGrid;
import com.boding.power.utils.DictUtils;
import com.boding.power.utils.QiNiuUtil;
import com.boding.power.utils.UserUtil;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

public class GoodsController extends Controller {
	public void pubGoodsUI() {
		render("pubGoods.jsp");
	}

	public void getImages() {
		Integer id = getParaToInt(0);
		GoodsPower goodsPower = GoodsPower.dao.findById(id);
		if (goodsPower != null && StringUtils.isNotBlank(goodsPower.getPics())
				&& StringUtils.isNotEmpty(goodsPower.getPics())) {
			String[] images = goodsPower.getPics().split(" ");
			renderJson(images);
		} else {
			renderNull();
		}
		
	}

	public void uploadImages() {
		List<String> fileKeys = new ArrayList<String>();
		List<UploadFile> uploadFiles = getFiles();
		for (UploadFile file : uploadFiles) {
			File f = file.getFile();
			String result = QiNiuUtil.uploadFile(f);
			if (StringUtils.isNotEmpty(result)
					&& StringUtils.isNotBlank(result)) {
				fileKeys.add(result);
			}
			f.delete();
		}
		renderJson(fileKeys);
	}

	public void delImage() {
		String key = getPara("key");
		Integer id = getParaToInt("id");
		if (StringUtils.isNotEmpty(key) && StringUtils.isNotBlank(key)) {
			QiNiuUtil.delFile(key);
			// 更新数据库商品的图片信息
			GoodsPower goodsPower = GoodsPower.dao.findById(id);
			String pics = goodsPower.getPics();
			String newPics = pics.replace(key, "");
			goodsPower.setPics(newPics);
			goodsPower.update();
			setAttr("key", key);
			renderText(key);
		} else {
			renderText("删除失败");
		}
	}

	public void pubGoods() {
		GoodsPower goodsPower = getModel(GoodsPower.class, "goods", true);
		if (StringUtils.isNotEmpty(goodsPower.getPics())
				&& StringUtils.isNotBlank(goodsPower.getPics())) {
			String pics = goodsPower.getPics().trim();
			goodsPower.setPics(pics);
		}
		goodsPower.setUserId(UserUtil.getUser().getId());
		goodsPower.setCreateDate(new Date());
		goodsPower.setStatus(PropKit.getInt("pub_strategy"));
		goodsPower.setDelFlag(Constants.FLAG_NORMAL);
		goodsPower.save();
		redirect("/admin/goods/listGoodsUI");
	}

	public void editGoods() {
		GoodsPower goodsPower = getModel(GoodsPower.class, "goods", true);
		String pics = goodsPower.getPics();
		if (StringUtils.isNotEmpty(pics) && StringUtils.isNotBlank(pics)) {
			GoodsPower goods = GoodsPower.dao.findById(goodsPower.getId());
			String newPics = goods.getPics() + pics;
			goodsPower.setPics(newPics.trim());
		} else {
			GoodsPower goods = GoodsPower.dao.findById(goodsPower.getId());
			goodsPower.setPics(goods.getPics().trim());
		}
		goodsPower.update();
		redirect("/admin/goods/listGoodsUI");
	}

	public void listGoodsUI() {
		render("goodsList.jsp");
	}

	public void listGoods() {
		String searchField = getPara("searchField");
		String searchString = getPara("searchString");
		Page<GoodsPower> pages = GoodsPower.dao.findByPage(
				getParaToInt("page"), getParaToInt("rows"), searchField,
				searchString);
		DataGrid<GoodsPower> dataGrid = new DataGrid<GoodsPower>(pages);
		renderJson(dataGrid);
	}

	public void operateGoods() {
		String operateType = getPara("oper");
		if ("edit".equals(operateType)) {//编辑
			Integer id = getParaToInt("id");
			GoodsPower goodsPower = GoodsPower.dao.findById(id);
			String pics = goodsPower.getPics();
			if (StringUtils.isNotBlank(pics) && StringUtils.isNotEmpty(pics)) {
				String[] picsList = pics.split(" ");
				setAttr("pics", picsList);
			}
			setAttr("goods", goodsPower);
			render("editGoods.jsp");
		} else if ("del".equals(operateType)) {//删除
			Integer id = getParaToInt("id");
			GoodsPower goodsPower = GoodsPower.dao.findById(id);
			goodsPower.setDelFlag(Constants.FLAG_DEL);
			goodsPower.update();
			renderText("success");
		} else if("pass".equals(operateType)){ //审核
			String ids = getPara("ids");
			if(StringUtils.isNotBlank(ids)&&StringUtils.isNotEmpty(ids)){
				String[] idArray = ids.split(",");
				for (int i = 0; i < idArray.length; i++) {
					GoodsPower goods = GoodsPower.dao.findById(Integer.parseInt(idArray[i]));
					goods.setStatus(Constants.PUB_NORMAL);
					goods.update();
				}
			}
			renderText("success");
		} else if("down".equals(operateType)){//下架
			String ids = getPara("ids");
			if(StringUtils.isNotBlank(ids)&&StringUtils.isNotEmpty(ids)){
				String[] idArray = ids.split(",");
				for (int i = 0; i < idArray.length; i++) {
					GoodsPower goods = GoodsPower.dao.findById(Integer.parseInt(idArray[i]));
					goods.setStatus(Constants.PUB_DOWN);
					goods.update();
				}
			}
			renderText("success");
		}
	}
	
	public void listGoodsCommentsUI(){
		render("goodsCommentsList.jsp");
	}
	public void listCommentsUI(){
		Integer id = getParaToInt(0);
		setAttr("goods_id", id);
		render("commentList.jsp");
	}
	
	public void listComments(){
		String searchField = getPara("searchField");
		String searchString = getPara("searchString");
		Integer id = getParaToInt(0);
		DataGrid<CommentDto> dataGrid = Comment.dao.findByPage(
				getParaToInt("page"), getParaToInt("rows"), searchField,
				searchString,id);
		renderJson(dataGrid);
	}
	
	public void editCommentUI(){
		Integer id =getParaToInt(0);
		Comment comment = Comment.dao.findById(id);
		setAttr("comment", comment);
		render("editComment.jsp");
	}
	
	public void editComment(){
		Integer id = getParaToInt("id");
		String content = getPara("content");
		Comment c = Comment.dao.findById(id);
		c.setContent(content);
		c.update();
		renderText("success");
	}
	
	public void delComment(){
		Integer id = getParaToInt(0);
		Comment c = Comment.dao.findById(id);
		c.setDelFlag(Constants.FLAG_DEL);
		c.update();
		renderText("success");
	}
}
